package com.yc.projectDemo;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.CookieStore;
import java.util.HashMap;
import java.util.Map;

public class NewsScraperToPDF {
    public static void main(String[] args) {
        String url = "http://www.nhc.gov.cn/wjw/xwdt/list.shtml";
        String pdfPath = "NHC_News.pdf";

        try {

			// 1. 先请求主页，获取最新 Cookies
			Connection.Response homePageResponse = Jsoup.connect("http://www.nhc.gov.cn/")
				.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36")
				.execute();

			Map<String, String> cookies = homePageResponse.cookies();

			// 2. 带着 Cookies 再次请求数据页面
			org.jsoup.nodes.Document doc = Jsoup.connect("http://www.nhc.gov.cn/wjw/index.shtml")
				.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36")
				.cookies(cookies)
				.get();

			System.out.println(doc.title());



//			Map<String, String> cookiesMap = new HashMap<>();
//			cookiesMap.put("sVoELocvxVW0S", "5u3VAGkQSkHvi.zC_G6OECpd0v7VX5vO5Wp3JU_N3vRj4Sk9TeDMY1SCUgUVT9XxGEei9fKJWmZXgpUkkFQa49G");
//			cookiesMap.put("insert_cookie", "91349450");
//			cookiesMap.put("arialoadData", "true");
//			cookiesMap.put("sVoELocvxVW0T", "5R5RW9CIbsoEqqqDYCygX0av4yNabyIWErnZSWmkXhPxh_cW5of4HSoxlke_nS7i9PMA3FJ3gxmOhhrviX075EMnwhcHtqaJ0txXKaDGcWNKYAdtz");
//
//            // 1. 发送请求，模拟浏览器访问
//            Connection.Response response = Jsoup.connect(url)
//				.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
//				.header("Accept-Language", "zh-CN,zh;q=0.9")
//				.header("Cache-Control", "max-age=0")
//				.header("Proxy-Connection", "keep-alive")
//				.header("Upgrade-Insecure-Requests", "1")
//				.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36")
//				.cookies(cookiesMap)
//				.timeout(5000)
//				.execute();
//
//			System.out.println(response.statusCode());



//            if (response.statusCode() == 200) {
//				org.jsoup.nodes.Document doc = response.parse();
//                Elements newsItems = doc.select(".list > ul > li"); // 选择新闻列表中的每一项
//
//                // 2. 创建 PDF 文档
//                File pdfFile = new File(pdfPath);
//                PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfFile));
//                com.itextpdf.layout.Document pdfDocument = new Document(pdfDoc);
//
//                // 3. 解析新闻数据
//                for (Element item : newsItems) {
//                    Element link = item.selectFirst("a"); // 新闻标题和链接
//                    Element dateElement = item.selectFirst("span"); // 新闻日期
//
//                    if (link != null && dateElement != null) {
//                        String title = link.text();
//                        String newsUrl = "http://www.nhc.gov.cn" + link.attr("href");
//                        String date = dateElement.text();
//
//                        // 4. 输出到 PDF
//                        pdfDocument.add(new Paragraph("标题: " + title));
//                        pdfDocument.add(new Paragraph("URL: " + newsUrl));
//                        pdfDocument.add(new Paragraph("日期: " + date));
//                        pdfDocument.add(new Paragraph("--------------------------------------"));
//                    }
//                }
//
//                // 5. 关闭 PDF 文档
//                pdfDocument.close();
//                System.out.println("PDF 生成成功: " + pdfPath);
//            } else {
//                System.out.println("请求失败，状态码: " + response.statusCode());
//            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
